package com.weipai.springboot.util;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransOrderQueryModel;
import com.alipay.api.domain.AlipayFundTransToaccountTransferModel;
import com.alipay.api.request.AlipayFundTransOrderQueryRequest;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransOrderQueryResponse;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;

import java.util.Map;

/**
 * 支付宝提现
 *
 */
public class AliPayTXUtil {
    
    private final static String APP_ID = "2017112200100819";
    //应用私钥
    private final static String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDT9gahpjq0MDGJoUfOY2JeK7x4TWePh30BtCSntgxvyoTrfa3sD2NE14Bo/hPIc4iVRV21kLZQQCspXy+d7d5cvkvKK4PQ+LVJRZyAr8AJg+U7z+P+VGTcyY/bQqPg+kzr33qnrt6AMLzGJQWehtlmK7wCsXxBCFLBlB7YurJo+5lfZesls8jdPcv1POWq9pHk0ZH5WTqaWiMtBWCEhsFLnYzoR6Ko9pqawGjjG8Uwp0y/l89dSyCHiMAssnZWrHHmSbwnKyumUX1Pw71jNOSVZ6xCQtI+UAfsX/c6ZHnv5y5DhXVqjukIPjLs/gQdEV3wBE2FWBNz6hhTsifAqCLVAgMBAAECggEAPMSLMCr9J42tlhyz/nrFx+PYG9FtQFwCvctMuvxZYm94Xh9hDo1201TJTtCREWX2IX2KmTRM4IMVHL5aY3e/yW0RQ3voRbAYIVKgxNAeSVwUC2rVnaPKavYqlRzs6OI5zWH7wzlCNxRB+rjAfKuChmVBeLAzxF1DhJYXpeEYpzn8psfJDGYKLju/S2fG2ysQin0R2Wxwzuz4AT9zaxuXzf8fEUq+NbVhhu9G7KvHf5UTOqumZzmMkGN8AhNzFcOpmceQlIbhhonhCyMUFkMmk1fuznlRoR5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2UEArKV8vne3eXL5LyiuD0Pi1SUWcgK/ACYPlO8/j/lRk3MmP20Kj4PpM6996p67egDC8xiUFnobZZiu8ArF8QQhSwZQe2LqyaPuZX2XrJbPI3T3L9TzlqvaR5NGR+Vk6mlojLQVghIbBS52M6EeiqPaamsBo4xvFMKdMv5fPXUsgh4jALLJ2Vqxx5km8JysrplF9T8O9YzTklWesQkLSPlAH7F/3OmR57+cuQ4V1ao7pCD4y7P4EHRFd8ARNhVgTc+oYU7InwKgi1QIDAQAB";
    /*支付宝公钥*/
    public final static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjAO2ag46vb4Yfr/MNDdhalg1S8W67vzz9tgKv3ofMMRJb9c+AYh5ING9T0XHDhaq18CtQHF224zZJPl7hArRDER2NKMmHm/JJUAXBbG0lPgxzLxMRh13esM9d2S06C9+q+i1ZceEs6e2jlw4ay62ogNke2Tp4XLrdJA/PpUBYXlgcMZJjPd1Ih3JwJJDmOJOmAT/J66kQLSepvne5vweNScjOm7zKVM6XUqMzTG/5UHVqBWiuUF7tZRz1QnzsmZ/fWFPY15dTC3dSClHeavVvmf3NUp7L34u1u+DqcwsoDm9F0BAEwEAOh0O4lg89ZD2rZfx+87oj0O5oSKFgmloBwIDAQAB";

    private final static String ALIPAY_URL = "https://openapi.alipay.com/gateway.do";
    private final static String ALIPAY_FORMAT = "json";
    private final static String ALIPAY_CHARSET = "UTF-8";
    private final static String SIGN_TYPE = "RSA2";
    
    private final static String ZZ_URL = "http://alipay.fund.trans.toaccount.transfer";
    

    public static AlipayClient alipayClient = new DefaultAlipayClient(ALIPAY_URL, APP_ID, APP_PRIVATE_KEY, ALIPAY_FORMAT, ALIPAY_CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

    /**
     * 单笔转账到支付宝账户
     * https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.54Ty29&treeId=193&articleId=106236&docType=1
     * @return
     * @throws AlipayApiException
     */
    public static boolean transfer(AlipayFundTransToaccountTransferModel model) throws AlipayApiException {
        AlipayFundTransToaccountTransferResponse response = transferToResponse(model);
        String result = response.getBody();
        System.out.println("transfer result>"+result);
        if (response.isSuccess()) {
            return true;
        } else {
            //调用查询接口查询数据
            JSONObject jsonObject = JSONObject.parseObject(result);
            String out_biz_no = jsonObject.getJSONObject("alipay_fund_trans_toaccount_transfer_response").getString("out_biz_no");
            AlipayFundTransOrderQueryModel queryModel = new AlipayFundTransOrderQueryModel();
            model.setOutBizNo(out_biz_no);
            boolean isSuccess = transferQuery(queryModel);
            if (isSuccess) {
                return true;
            }
        }
        return false;
    }

    public static AlipayFundTransToaccountTransferResponse transferToResponse(AlipayFundTransToaccountTransferModel model) throws AlipayApiException{
        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        request.setBizModel(model);
        return alipayClient.execute(request);
    }

    /**
     * 转账查询接口
     * @return
     * @throws AlipayApiException
     */
    public static boolean transferQuery(AlipayFundTransOrderQueryModel model) throws AlipayApiException{

        AlipayFundTransOrderQueryResponse response = transferQueryToResponse(model);
        System.out.println("transferQuery result>"+response.getBody());
        if(response.isSuccess()){
            return true;
        }
        return false;
    }

    public static AlipayFundTransOrderQueryResponse transferQueryToResponse(AlipayFundTransOrderQueryModel model) throws AlipayApiException{
        AlipayFundTransOrderQueryRequest request = new AlipayFundTransOrderQueryRequest();
        request.setBizModel(model);
        return alipayClient.execute(request);
    }
}
